<template>
  <div>
    <!-- 添加按钮 -->
    <div class="btn">
      <el-button size="small" type="success" @click="addShow">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="unitList" style="width: 100%">
      <el-table-column prop="name" label="名称" width="550px" />
      <el-table-column prop="createTime" label="创建时间" width="550px" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[1, 2, 3, 10]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="loadData"
      @current-change="loadData"
      style="margin-top: 20px"
    />
    <!-- 弹框 -->
    <el-dialog
      v-model="dialogFormVisible"
      title="添加或修改"
      style="width: 700px"
    >
      <el-form :model="unit">
        <el-form-item label="名称" label-width="100px">
          <el-input v-model="unit.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = dialogShowRole = false">
            取消
          </el-button>
          <el-button type="primary" @click="updateOrAdd">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {
  GetUnitList,
  GetUnitById,
  DeleteUnit,
  UpdateUnit,
  SaveUnit,
} from '@/api/productUnit'
import { ElMessage, ElMessageBox } from 'element-plus'
const pageNum = ref(1)
const pageSize = ref(3)
const total = ref(0)
const unitList = ref([])
const unit = ref({})
const loadData = async () => {
  const { data } = await GetUnitList(pageNum.value, pageSize.value)
  unitList.value = data.list
  total.value = data.total
}
onMounted(() => {
  loadData()
})

//弹窗属性
const dialogFormVisible = ref(false)
//添加或修改
const addShow = () => {
  unit.value = {}
  dialogFormVisible.value = true
}
const handleEdit = async row => {
  const { data } = await GetUnitById(row.id)
  unit.value = data
  dialogFormVisible.value = true
}
const handlerSave = async () => {
  await SaveUnit(unit.value)
  loadData()
  dialogFormVisible.value = false
  ElMessage.success('添加商品单位成功')
}
const handlerUpdate = async () => {
  await UpdateUnit(unit.value)
  dialogFormVisible.value = false
  loadData()
  ElMessage.success('修改商品单位成功')
}
const updateOrAdd = () => {
  if (unit.value.id) {
    //修改
    handlerUpdate()
  } else {
    //添加
    handlerSave()
  }
}
//删除商品单位
const handleDelete = row => {
  ElMessageBox.confirm(`确认删除【${row.name}】商品单位吗?`, 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      await DeleteUnit(row.id)
      loadData()
    })
    .catch(() => {
      ElMessage({
        type: 'success',
        message: '取消删除',
      })
    })
}
</script>
<style>
.btn {
  background-color: #fff;
  height: 50px;
  line-height: 50px;
  margin-bottom: 15px;
  padding-left: 15px;
}
</style>
